<template>
  <div class='container'>
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id" class="breadcrum">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>

      <el-form :model="catalog" label-width="80px" size="small" ref="elFormList">
        <el-row>
          <!-- 目录名称 -->
          <el-col :span="6">
            <el-form-item label="目录名称" prop='directoryName'>
              <el-input v-model="catalog.directoryName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态" prop='state'>
              <el-select v-model="catalog.state" placeholder="请选择">
                <el-option
                  v-for="item in status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
              <el-button size="small" @click="clearList">清除</el-button>
              <el-button type="primary" size="small" @click="searchList">搜索</el-button>
          </el-col>
            <el-col :span="6" class="newAdd">
              <el-button
                v-show="$route.query.id"
                size="mini" type="text"
                icon="el-icon-back"
                @click="$router.push('/subjects/list')"
              >返回学科</el-button>
              <el-button class="add-btn" icon="el-icon-edit" type="success" size="small" @click="addForm()">新增目录</el-button>
            </el-col>
        </el-row>
      </el-form>
      <!-- 提示框 -->
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        :closable= "false"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table class="tableList" :data="dataList" style="width: 100%">
        <el-table-column
          prop="date"
          label="序号"
          type="index"
          width="80">
        </el-table-column>
        <el-table-column
          prop="subjectName"
          label="所属学科"
          width="80">
        </el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="username"
          label="创建者">
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="创建日期"
          width="200">
          <template slot-scope="scope">
            {{scope.row.addDate | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totals"
          label="面试题数量"
          width="100"  
        >
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">已启用</span>
            <span v-if="scope.row.state === 0">已禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.state === 0">
              <el-link :underline="false" type="primary" @click="amendState(scope.row.id, scope.row.state)">禁用</el-link>
              <el-link :underline="false" type="info">修改</el-link>
              <el-link :underline="false" type="info">删除</el-link>
            </div>
            <div v-else>
              <el-link :underline="false" type="primary" @click="amendState(scope.row.id, scope.row.state)">启用</el-link>
              <el-link
                :underline="false"
                type="primary"
                @click="editTagShow(scope.row)"
              >修改</el-link>
              <el-link :underline="false" type="primary" @click="deleteListBtn(scope.row.id)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="text-align: right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="catalog.page"
          :page-sizes="[10, 20, 30]"
          :page-size="catalog.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts">
        </el-pagination>
      </div>
      <!-- 新增目录 -->
       <!-- :id="$router.query.id" -->
      <adddirectory ref="adddire" @handle="addMulu"></adddirectory>
      
      <!-- 修改目录弹出框 -->
      <el-dialog title="修改标签" :visible.sync="editTagDialogVisible" width="30%">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
           <!-- v-if="!$router.query.id" -->
          <el-form-item label="所属学科" prop="subjectID">
            <el-select v-model="editForm.subjectID" placeholder="请选择">
              <el-option :label="item.label" :value="item.value" v-for="item in simpleList" :key="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model.trim="editForm.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editTagDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editTag">确 定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
// 数据列表 删除 目录状态
import { list, remove, changeState, add, update } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
// 新增组件
import adddirectory from '../components/directorys-add'
import { simple } from '@/api/hmmm/subjects'
// 引入时间过滤器
import { parseTimeByString } from '@/filters'
 
export default {
  components: {
    adddirectory
  },
  data () {
    return {
      catalog: {
        // 目录名称
        directoryName: '',
        // 状态
        state: null,
        // 当前页
        page: 1,
        // 每页几条数据
        pagesize: 10,
        // 学科ID
        subjectID: this.$route.query.id
      },
      // 状态
      status,
      // 表格初始化数据
      dataList: [],
      // 数据条数
      counts: null,
      // 修改弹框
      editList: false,
      // 新增弹框
      newList: false,
      // 点击修改获取的当前数据
      edit: null,
      // 修改验证规则
      editFormRules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      // 修改弹出框
      editTagDialogVisible: false,
      addTagDialogVisible: false,
      // 修改表单数据
      editForm: {
        directoryName: '',
        subjectID: '',
        id: ''
      },
      // 简单数据列表
      simpleList: []
    }
  },
  created () {
    this.loadSimpleList()
    this.getDataList()
  },
  methods: {
    async getDataList () {
      const res = await list(this.catalog)
      if (res.status !== 200) {
        this.$message.error('获取数据失败')
      }
      // 数据总条数
      this.counts = res.data.counts
      // 数据列表
      this.dataList = res.data.items
    },
    // 清除
    clearList () {
      this.$refs.elFormList.resetFields()
      this.getDataList()
    },
    // 搜索
    searchList () {
      this.getDataList()
    },
    // 启用、禁用
    async amendState (id, state) {
      if (state === 0) {
        state = 1
      } else {
        state = 0
      }
      const data = {id, state}
      await changeState(data)
      this.getDataList()
    },
    // 删除
    deleteListBtn (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id })
          this.getDataList()
        })
        .catch(() => {
          this.$message.error('取消删除')
        })
    },
    // 每页的条数
    handleSizeChange (newPageSize) {
      this.catalog.pagesize = newPageSize
      this.getDataList()
    },
    // 点击更换页码
    handleCurrentChange (newpage) {
      this.catalog.page = newpage
      this.getDataList()
    },
    // 确定添加目录
    async addMulu (data) {
      await add(data)
      this.getDataList()
      this.$refs.adddire.form.subjectID = null,
      this.$refs.adddire.form.directoryName = null
    },
    addForm() {
      this.$refs.adddire.dialogFormVisible = true
    },
      // 获取简单学科列表
    async loadSimpleList () {
      const { data } = await simple()
      this.simpleList = data
    },
    // 确定修改
    async editTag (val) {
      this.editTagDialogVisible = false
      const { data: res } = await update(this.editForm)
      this.getDataList()
      this.$message.success('修改成功')
    },
    // 修改标签
    async editTagShow (val) {
      this.editTagDialogVisible = true
      this.editForm.subjectID = val.subjectID
      this.editForm.directoryName = val.directoryName
      this.editForm.id = val.id
    }
  }
}
</script>

<style scoped lang='less'>
  .container {
    .tableList {
      margin: 20px 0;
    }
    .breadcrum {
      margin-bottom: 20px;
    }
    .newAdd {
      text-align: right;
    }
    .el-link {
      margin-right: 10px;
    }
  }
</style>
